#ifndef PLAYER_H
#define PLAYER_H

#include <QGraphicsItem>
#include <QObject>
#include <QPainter>
#include <QKeyEvent>
#include "mouse.h"
#include "map.h"
#include "movingobject.h"
#include "gun.h"
#include "bullet.h"
#include "connectionplayer.h"
#include "clientconnection.h"

class Map;
class Gun;
class MovingObject;
class Bullet;
class ConnectionPlayer;
class ClientConnection;

class Player : public MovingObject
{
    Q_OBJECT
public:
    enum { Type = UserType + 1 };
    int type() const
    {
        return Type;
    }

    Player(qreal x, qreal y, ConnectionPlayer* pl);
    ~Player();
    QRectF boundingRect() const;
    QPainterPath shape() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    void changePos(QPointF p);
    void changePos(qreal x, qreal y);
    void changeXPos(qreal x);
    void changeYPos(qreal y);
    bool movePlayer();
    bool movePlayerBy(qreal dx, qreal dy);
    bool movePlayerBy(QLineF vector);
    bool movePlayerBy(QPointF vector);

    void decreaseHealth(qreal h);
    void setPlayerAngle(qreal ang);
    qreal getRadius();
    void updateKeys(int nr);

    void setGun(Gun *gun);
    Gun* getGun();

    void setMouse(Mouse *mouse);
    void unsetMouse();

    ConnectionPlayer* getConnectionPlayer();
    void setConnectionPlayer(ConnectionPlayer* cp);

    void setNick(QString n);
	void setScore(quint16 n);
	void setPlayerId(quint8 n);
	void setHealth(qreal h);
	void setUsesMouse(bool um);
	void setIsRemote(bool ir);

	QString getNick();
	quint16 getScore();
	quint8 getPlayerId();
	QPointF getPos();
	bool getIsAlive();
	bool getIsReloading();
	bool getIsShooting();
	qreal getAngle();
	qreal getSpeed();
	qreal getHealth();
	bool getUsesMouse();
	bool getIsRemote();
    quint32 getDeathTime();
    QPointF getVelocity();
    void setDeathTime(quint32 dt);

    Map *map();
    QVector<Bullet*> getBullets();
    void checkIsAlive();
protected:
    void keyPressEvent(QKeyEvent *evt);
    void keyReleaseEvent(QKeyEvent *evt);
private:

    ConnectionPlayer* connectionPlayer;

    Mouse *mouse;
    Gun *gun;
    qreal radius;

    qreal wsp_m;
    int keyToNr(int key);
    void aimAtMouse();
    void playerTimeTick();


public slots:
    void onMousePressed(QMouseEvent *evt);
    void onMouseReleased(QMouseEvent *evt);
    void onGunFired(Bullet *b);
    void setIsAlive(bool isAlive);
    void setPosition(QPointF pos);

private slots:
	void fireGun(qreal x, qreal y, qreal angle);
	void fireGun();
	void updateItemPosition();
	void setIsReloading(bool isReloading);
	void setIsShooting(bool iss);
	void setAngle(qreal angle);
	void setDirs(quint8, quint8);
	void onHitPlayer(Player*, Player*);

signals:
	void hitPlayer(Player*, Player*);
	void positionChanged(Player*);

};

#endif // PLAYER_H
